<?php
namespace app\common\command;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\Db;
use think\exception\ErrorException;

class BookMoney extends Command
{
    protected function configure()
    {
        $this->setName('book_money')
            ->addArgument('action', Argument::OPTIONAL, "方法名称")
            ->setDescription('书籍充值金额处理');
    }

    protected function execute(Input $input, Output $output)
    {

        $action = $input->getArgument('action');
        $this->$action();

    }

    /*书币充值金额折扣*/
    public function discount()
    {
        $date = date('Y-m-d', strtotime('-1days'));
        $field = "nid,uv,money,cuv,pay_uv,use_coin,new_uv,new_pay_uv,new_money,read_date";
        $page = 1;
        $record = Db::table("cps_setting")->where('type','book_discount_money')->field('data')->findOrEmpty();
        $book_discount_money = $record && strlen($record['data']) >= 1 ? $record['data'] : 0;

        if($book_discount_money < 1 || $book_discount_money == 100)
        {
            $discount = 1;
        }else{
            $discount = $book_discount_money / 100;
        }

        $recordList = Db::table("cps_setting")->where('type','book_discount_money_list')->field('data')->findOrEmpty();
        $book_discount_money_list = $recordList ? json_decode($recordList['data']) : [];
        $book_discount_list = $book_discount_money_list ? array_column($book_discount_money_list,'rate','nid') : [];

        do{

            $list = Db::table("rp_novel_date_daily")->where('read_date',$date)
                ->field($field)->page($page,300)->select();

            if($list)
            {
                foreach ($list as $vo)
                {

                    if(isset($book_discount_list[$vo['nid']]) && strlen($book_discount_list[$vo['nid']]) >= 1 )
                    {
                        if($book_discount_list[$vo['nid']] < 1 || $book_discount_list[$vo['nid']] == 100)
                        {
                            $discount_new = 1;
                        }else{
                            $discount_new = $book_discount_list[$vo['nid']] / 100;
                        }
                    }else{
                        $discount_new = $discount;
                    }

                    $data = [];

                    $data['nid']            = $vo['nid'];
                    $data['read_date']      = $vo['read_date'];
                    $data['uv']             = $vo['uv'] > 0 ? intval($vo['uv']*$discount_new) : 0;
                    $data['money']          = $vo['money'] > 0 ? intval($vo['money']*$discount_new) : 0;
                    $data['cuv']            = $vo['cuv'] > 0 ? intval($vo['cuv']*$discount_new) : 0;
                    $data['pay_uv']         = $vo['pay_uv'] > 0 ? intval($vo['pay_uv']*$discount_new) : 0;
                    $data['use_coin']       = $vo['use_coin'] > 0 ? intval($vo['use_coin']*$discount_new) : 0;
                    $data['new_uv']         = $vo['new_uv'] > 0 ? intval($vo['new_uv']*$discount_new) : 0;
                    $data['new_pay_uv']     = $vo['new_pay_uv'] > 0 ? intval($vo['new_pay_uv']*$discount_new) : 0;
                    $data['new_money']      = $vo['new_money'] > 0 ? intval($vo['new_money']*$discount_new) : 0;


                    $record_d = Db::table("rp_novel_date_daily_discount")->where('read_date',$date)
                        ->where('nid',$vo['nid'])->field('id')->findOrEmpty();
                    if($record_d)
                    {
                        Db::table("rp_novel_date_daily_discount")->where('id',$record_d['id'])->update($data);
                    }else{
                        Db::table("rp_novel_date_daily_discount")->insert($data);
                    }
                }
            }

            $page = $page + 1;
        }while($list);

    }


}